<?php
	// invoke connection
	require_once("conn.php");
	
	//admin login check
	function admin_login_check($username,$password){
		$sql = "select * from pcd_admins where username='".$username."' and password='".$password."';";		
		$result = mysql_query($sql);
		while($rs=mysql_fetch_object($result)){
			if(($rs->username)==$username){
				return true;
			}
		}
		return false;
	}

	//get the admin's id by his username password
	function getidbyusername($username,$password){
		$sql = "select aid from pcd_admins where username='".$username."' and password='".$password."';";
		$result = mysql_query($sql);
		while($rs=mysql_fetch_object($result)){			
			return $rs->aid;			
		}
		return "";		
	}
	
	//get scenes 
	function getscenes(){
		$sql = "select * from pcd_scences";
		return mysql_query($sql);		
	}
	
	//get scene by id
	function get_scene_by_id($cid){
		$sql = "select * from pcd_scences where cid = ".$cid;		
		return mysql_query($sql);
	}
	
	//new scene
	function scene_add($title, $content, $type, $area, $userid){	
		if($title!=""){
			$sql = "insert into pcd_scences values(null,'".$title."','".$content."','".$type."','".$area."',now(),'".$userid."')";			
			if(mysql_query($sql)==1){
				return 1;
			}
		}
		return 0;
	}
	
	//update scene
	function scene_update($cid, $title, $content, $type, $area, $userid){
		if($cid!=""){			
			//$sql = "insert into pcd_scences values(null,'".$title."','".$content."','".$type."','".$area."',now(),'".$userid."')";			
			$sql = "update pcd_scences set title='".$title."',content='".$content."',type='".$type."',area='".$area."', postdate=now(),postuser='".$userid."' where cid ='".$cid."';";			
			if(mysql_query($sql)==1){
				return 1;
			}
		}
		return 0;
	}
	
	//get max scene id
	function get_max_scene_id($userid){
		if($userid!=""){
			$sql = "select max(cid) as maxid from pcd_scences where postuser = ".$userid;			
			return mysql_query($sql);
		}
	}
	
	//insert image info to database
	function image_add($name, $url, $jid, $sort, $userid){	
		if($name!=""){
			$sql = "insert into pcd_images values(null,'".$name."','".$url."','".$jid."','".$sort."',now(),'".$userid."')";			
			if(mysql_query($sql)==1){
				return 1;
			}
		}
		return 0;
	}
	
	//delete scene by id
	function delete_scene_by_id($cid){
		if($cid!=""){
			$sql = "delete from pcd_scences where cid = ".$cid;
			if(mysql_query($sql)==1){
				$sql2 = "delete from pcd_images where sort = c and jid = ".$cid;				
				//$sql3 = "delete from pcd_sights where cid = ".$cid;
				mysql_query($sql2);
				//mysql_query($sql3);
				return 1;
			}
		}
	}
	
	//new sight
	function sight_add($title, $content, $userid, $cid){	
	
		if($title!=""){
			$sql = "insert into pcd_sights values(null,'".$title."','".$content."',now(),'".$userid."','".$cid."')";			
			if(mysql_query($sql)==1){
				return 1;
			}
		}
		return 0;
	}
	
	//get max sight id
	function get_max_sight_id($userid,$cid){
		if($userid!=""){
			$sql = "select max(sid) as maxid from pcd_sights where postuser = ".$userid." and cid = ".$cid;			
			return mysql_query($sql);
		}
	}
	
	//get sights by scene id 
	function get_sights_by_cid($cid){
		$sql = "select * from pcd_sights where cid = ".$cid;
		return mysql_query($sql);		
	}	
	
	//delete sight by id
	function delete_sight_by_id($sid){
		if($sid!=""){
			$sql = "delete from pcd_sights where sid = ".$sid;
			if(mysql_query($sql)==1){
				$sql2 = "delete from pcd_images where sort = s and jid = ".$sid;				
				//$sql3 = "delete from pcd_sights where cid = ".$cid;
				mysql_query($sql2);
				//mysql_query($sql3);
				return 1;
			}
		}
	}
	
	//get sight by id
	function get_sight_by_id($sid){
		$sql = "select * from pcd_sights where sid = ".$sid;		
		return mysql_query($sql);
	}
	
	//update sight
	function sight_update($sid, $title, $content, $userid, $cid){
		if($sid!=""){			
			//$sql = "insert into pcd_scences values(null,'".$title."','".$content."','".$type."','".$area."',now(),'".$userid."')";			
			$sql = "update pcd_sights set title='".$title."',content='".$content."', postdate=now(),postuser='".$userid."', cid='".$cid."' where sid ='".$sid."';";	
			echo $sql." <<<<<<<<<<<<<<<<<<<<<<<<<<";		
			if(mysql_query($sql)==1){
				return 1;
			}
		}
		return 0;
	}
	
	//////////////////////////////////////////////////////////////////////////////////////
	function contact_add($name,$mobile,$info,$userid){	
		$sql = "insert into cts_contact(name,mobile,info,createtime,userid) values('".$name."','".$mobile."','".$info."',now(),'".$userid."')";			
		if($name!=""){			
			if(mysql_query($sql)==1){			
				return 1;
			}
		}
		return 0;
	}
	
	//????? ?????id
	function deletebyid($contactid){
		$sql = "delete from cts_contact where contactid=".$contactid;		
		if(mysql_query($sql)==1){
			return 1;
		}
		return 0;
	}
	
	//????? ????id
	function deleteallbyuserid($userid){
		$sql = "delete from cts_contact where userid=".$userid;		
		if(mysql_query($sql)==1){
			return 1;
		}
		return 0;
	}
	
	//?????
	function updatebyid($contactid,$name,$mobile,$info){
		$sql = "update cts_contact set name='".$name."', mobile='".$mobile."', info='".$info."' where contactid=".$contactid.";";	
		if(mysql_query($sql)==1){
			return 1;
		}
		return 0;
	}
	
	//????
	function userbyid($userid){
		$sql = "select * from cts_users where userid=".$userid.";";
		return mysql_query($sql);
	}
	
	//?????
	function getcontactsbyid($userid){
		$sql = "select * from cts_contact where userid=".$_SESSION['userid'];
		return mysql_query($sql);		
	}
	
	//?????
	/*
	function getcontactscount($userid){
		$sql = "select count(*) from cts_contact where userid=".$_SESSION['userid'];
		return mysql_query($sql);	
	}
	*/
	//??????
	function updatebyuserid($userid,$username,$name,$email,$mobile){
		$sql = "update cts_users set username='".$username."',name='".$name."',email='".$email."',mobile='".$mobile."' where userid=".$userid.";";	
		if(mysql_query($sql)==1){
			return 1;
		}
		return 0;
	}
	//??????
	function updatepasswordbyuserid($userid,$new){
		$sql = "update cts_users set password='".$new."' where userid=".$userid.";";	
		if(mysql_query($sql)==1){
			return 1;
		}
		return 0;		
	}
?>